<?php
// +----------------------------------------------------------------------
// | 菜单权限业务
// +----------------------------------------------------------------------
namespace app\admin\service;

use app\common\model\SysMenuModel;
use app\common\service\SysMenuService as CommonSysMenuService;
use app\common\enum\BaseStatusEnum;
use app\common\enum\ErrorEnum;

class SysMenuService {
    /**
     * 分页
     * @param  array  $param  请求参数
     * @return array
     */
    public static function list($params = []){
        $model = new SysMenuModel();
        if (isset($params['status']) && $params['status']) {
            $model = $model->where('status', $params['status']);
        } 
        $resList = $model->order('sort desc, id asc')->select()->toArray();
        foreach ($resList as $k => $v) {
            $resList[$k]['statusName'] = BaseStatusEnum::getDescription($v['status']);
        }

        $list = treeData($resList);

        return self::showMenuList($list);
    }

    public static function showMenuList($list) {
        // 最多三级
        foreach ($list as $k => $v) {
            $list[$k]['space'] = '';
            if ($v['children']) {
                $index = 0;
                foreach ($v['children'] as $kk => $vv) {
                    if ((count($v['children']) - 1) == $index) {
                        $list[$k]['children'][$kk]['space'] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└─';
                    } else {
                        $list[$k]['children'][$kk]['space'] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├─';
                    }
                    $index++;

                    if ($vv['children']) {
                        $index2 = 0;
                        foreach ($vv['children'] as $kkk => $vvv) {
                            if ((count($vv['children']) - 1) == $index2) {
                                $list[$k]['children'][$kk]['children'][$kkk]['space'] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└─';
                            } else {
                                $list[$k]['children'][$kk]['children'][$kkk]['space'] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├─';
                            }
                            $index2++;
                        }
                    }
                }
            }
        }
        return flattenMenuArray($list);
    }


    /**
     * 新增数据
     * @param  array  $data  新增的数据
     * @return json
     */
    public static function save($data){
        if(SysMenuModel::getByMenuName($data['menu_name'])){
            return failure(ErrorEnum::DATA_IS_EXIST_CODE, '菜单名字已经存在');
        }
        
        return CommonSysMenuService::save($data);
    }

    /**
     * 更新数据
     * @param  array  $data  更新的数据
     * @return json
     */
    public static function update($data){
        $info = SysMenuModel::find($data['id']);
        if(!$info){
            return failure(ErrorEnum::F_IS_NOT_EXIST_CODE, ErrorEnum::F_IS_NOT_EXIST_DESC);
        }

        $info->save($data);
        return $info->id;
    }

    /**
     * 详情
     * @param  array  $id  数据ID
     * @return json
     */
    public static function info($id){
        $data = CommonSysMenuService::info($id);
        if ($data['parent_id'] == 0) {
            $data['parent_menu_name'] = "顶级菜单";
        } else {
            $data['parent_menu_name'] = SysMenuModel::where('id', $data['parent_id'])->value('menu_name');
        }

        return $data;
    }

    /**
     * 删除
     * @param  string  $ids  数据ID
     * @return json
     */
    public static function destroy($ids){
        if(!empty($ids)){
            SysMenuModel::deleteById($ids);
        }
    }

    
}